"""二次封装"""
import configparser
import pymysql
class Zhao(object):
    def __init__(self,ini_path):
        self.ini_path=ini_path
    #定义方法
    """封装ini文件引入方法"""
    def get_ini_data(self):
        cfg = configparser.ConfigParser()  # 创建ini操作对象
        cfg.read(self.ini_path, encoding='utf8')  # 读取db.ini文件
        ini_dict_data = dict(cfg.items('mysqldb'))  # 获取sqldb节下的数据并转换为字典
        ini_dict_data['port'] = 3306  # 修改字典  字典名[建]=新值
        return ini_dict_data
    """查看数据库相关方法"""
    def show_data(self,your_sql):
        cnn = pymysql.connect(**self.get_ini_data())  # 将ini_dict_data 字典拆包使用
        #用上下文管理器
        with cnn:
            with cnn.cursor(cursor=pymysql.cursors.DictCursor) as cur:
              #其他操作
              #准备sql语句
              my_sql=your_sql
              #执行sql语句
              try:
                cur.execute(my_sql)
              except Exception as e:
                return '请检查你的sql语句是否准确',e

              else:
              #查看执行结果
                  data=cur.fetchall()
                  return data





    '''适用于 修改的 删除  插入'''
    def  change_data(self,your_sql):

        cnn = pymysql.connect(**self.get_ini_data())  # 将ini_dict_data 字典拆包使用
        with cnn:
            with cnn.cursor(cursor=pymysql.cursors.DictCursor) as cur:
                # 其他操作
                # 准备sql语句
                my_sql = your_sql
                try:
                    aff1=cur.execute(my_sql)
                except Exception as e:
                    return '请检查你的代码是否正确',e
                else:
                    if aff1 != 0:
                        cnn.commit()
                        return '修改成功'
                    else:
                        cnn.rollback()
                        return '修改失败'
                finally:
                    pass
    """添加"""
    def  add_data(self,your_sql):
        cnn = pymysql.connect(**self.get_ini_data())  # 将ini_dict_data 字典拆包使用
        with cnn:
            with cnn.cursor(cursor=pymysql.cursors.DictCursor) as cur:
                # 其他操作
                # 准备sql语句
                my_sql = your_sql
                try:
                    aff1=cur.execute(my_sql)
                except Exception as e:
                    return '请检查你的代码是否正确',e
                else:
                    if aff1 != 0:
                        cnn.commit()
                        return '添加成功'
                    else:
                        cnn.rollback()
                        return '添加失败'
                finally:
                    pass
    """删除"""
    def del_data(self, your_sql):
        cnn = pymysql.connect(**self.get_ini_data())  # 将ini_dict_data 字典拆包使用
        with cnn:
            with cnn.cursor(cursor=pymysql.cursors.DictCursor) as cur:
                # 其他操作
                # 准备sql语句
                my_sql = your_sql
                try:
                    aff1 = cur.execute(my_sql)
                except Exception as e:
                    return '请检查你的代码是否正确', e
                else:
                    if aff1 != 0:
                        cnn.commit()
                        return '删除成功'
                    else:
                        cnn.rollback()
                        return '删除失败'
                finally:
                    pass







if __name__ == '__main__':
    #实例化对象
    x1=Zhao('../data/data.ini')
    #操作对象
    show_sql="SELECT birthday FROM `ecs_users` where user_name='liu999';"
    print(x1.show_data(show_sql))
    # change_sql="SELECT birthday FROM `ecs_users` where user_name='liu999';"
    # print(change_sql)
    # print(x1.change_data(change_sql))
    # add_sql="insert into news values (1,'唐山事件','刘备','拒绝暴利','2022.6.21',29);"
    # print(x1.add_data(add_sql))
    # del_sql="delete from news where title='唐山事件'; "
    # print(x1.del_data(del_sql))
